﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ActionSystem.Core
{
    /// <summary>
    /// 与状态无关的全局Action，
    /// buff
    /// 或者其他个体与该个体进行数据交互或者
    /// </summary>
    public sealed class BasePassiveAction
    {
        bool inited = false;
        public bool Inited => inited;

        public BasePassiveActionConfig config;
        public BasePassiveActionConfig Config => config;
        public BasePassiveActionHandler handler;
        public BasePassiveActionHandler Handler => handler;
        public BasePassiveAction(BasePassiveActionConfig config)
        {
            this.config = config;
            this.handler = GetActionHandler(config.GetType()); 
        }
        /// <summary>
        /// 初始化
        /// </summary>
        public void Init(BaseStateMachine stateMachine)
        {
            Handler?.Begin(Config,stateMachine);
            inited = true;
        }
        bool ended = false;
        public bool Ended => ended;
        /// <summary>
        /// 设置可以被移除
        /// </summary>
        public void Remove(BaseStateMachine stateMachine)
        {
            Handler?.End(Config,stateMachine);
            ended = true;
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="stateMachine">当前状态机</param>
        /// <param name="updateLogicAction">是否更新到下一个逻辑帧</param>
        /// <param name="deltaTime"></param>
        public void Update(BaseStateMachine stateMachine, bool updateLogicAction, float deltaTime)
        {
            Handler?.Update(Config,stateMachine,updateLogicAction,deltaTime);
        }
        /// <summary>
        /// 判断是否结束
        /// </summary>
        /// <param name="stateMachine"></param>
        /// <returns></returns>
        public bool CheckEnd(BaseStateMachine stateMachine)
        {
            if(Handler==null)
            {
                return false;
            }
            return Handler.CheckEnd(Config, stateMachine);
        }

        /// <summary>
        /// 获取handler
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static BasePassiveActionHandler GetActionHandler(Type type)
        {
            BasePassiveActionHandler handler = null;

            //具有该参数的类type->Attribute->IIActionHandler
            PassiveActionConfigAttribute config = type.GetCustomAttribute<PassiveActionConfigAttribute>(true);

            Type handlerType = config.handlerType;
            //实例化该类
            handler = Activator.CreateInstance(handlerType) as BasePassiveActionHandler;
            if (handler == null)
            {
                throw new System.Exception($"{handlerType} 类型未继承 {nameof(BasePassiveActionHandler)} 接口");
            }
            return handler;
        }
    }
}
